package com.vichat.common.util;

import java.security.SecureRandom;
import java.util.concurrent.atomic.AtomicInteger;

public class IdGenerator {

    public static final char[] ALPHLEBET = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    private static AtomicInteger index = new AtomicInteger(500);

    public synchronized static String getEduId() {
        return getId("EDU");
    }

    public synchronized static String getRecId() {
        return getId("REC");
    }

    public synchronized static String getLogId() {
        return getId("LOG");
    }

    public synchronized static String getJobId() {
        return getId("JOB");
    }

    public synchronized static String getHobbyId() {
        return getId("HBY");
    }



    public static String getIDCard() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 9; i++) {
            int idx = (int) Math.round(Math.random() * 51);
            builder.append(ALPHLEBET[idx]).append(Math.round(Math.random() * 9));
        }
        return builder.toString();
    }

    public static String fileId() {
        return getId("cf_");
    }

    public static String momentId(long uid) {
        return getId(uid + "");
    }

    public static String commentId() {
        return getId("cmt_");
    }

    public static String smsId() {
        return getId("sms_");
    }

    public static String smsCode() {
        SecureRandom random = new SecureRandom();
        int code = 0;
        while (code < 1000) {
            code = random.nextInt(10000);
        }
        return String.valueOf(code);
    }

    /**
     * 每毫秒生成100个ID
     *
     * @param type  type.length <= 3
     * @return 18位长度字符串
     */
    private static String getId(String type) {
        int i = index.incrementAndGet();
        String ii = i < 10 ? ("000" + i) : i < 100 ? "00" + i : i < 1000 ? "0" + i : i + "";
        if (i > 999) {
            index.set(0);
        }
        StringBuilder builder = new StringBuilder();
        builder.append(type).append(System.currentTimeMillis()).append(ii);
        builder.append(ALPHLEBET[(int) Math.round(Math.random() * 51)]).append(ALPHLEBET[(int) Math.round(Math.random() * 51)]);
        return builder.toString();
    }

    public static String createMid() {

        int i = index.incrementAndGet();
        String ii = i < 10 ? ("000" + i) : i < 100 ? "00" + i : i < 1000 ? "0" + i : i + "";
        if (i > 999) {
            index.set(0);
        }

        StringBuilder builder = new StringBuilder();
        builder.append(System.currentTimeMillis()).append(ii);
        builder.append(ALPHLEBET[(int) Math.round(Math.random() * 51)]).append(ALPHLEBET[(int) Math.round(Math.random() * 51)]);
        return builder.toString();
    }


    public synchronized static String getRefId() {



        StringBuilder builder = new StringBuilder();
        builder.append(System.currentTimeMillis());
        builder.append(ALPHLEBET[(int) Math.round(Math.random() * 51)]).append(ALPHLEBET[(int) Math.round(Math.random() * 51)]).append(ALPHLEBET[(int) Math.round(Math.random() * 51)]);
        return builder.toString();
    }

    public static void main(String[] args) {
//        for (int i = 0; i < 100; i++) {
//            Thread t = new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    System.err.println(createMid());
//                }
//            });
//            t.start();
//        }
//        long x = System.currentTimeMillis() % 1000000000000L;
//        System.out.println(x);
//        System.out.println(x / 1000 / 60 / 60 / 24);
//        System.out.println(System.currentTimeMillis());
//        System.out.println(createMid().length());
        String refId= getRefId();
        System.out.println(refId);
        System.out.println(refId.length());
    }

}
